package com.deyuanyun.pic.pipe.service.impl;

import com.deyuanyun.pic.cache.SessionCacheSupport2;
import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.ConvertUtil;
import com.deyuanyun.pic.common.util.MyCollectionUtil;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.controller.dto.UserSessionVO;
import com.deyuanyun.pic.pipe.domain.PicPipeIdentification;
import com.deyuanyun.pic.pipe.domain.PicPipeIdentificationG;
import com.deyuanyun.pic.pipe.domain.PicPipeIdentificationGExample;
import com.deyuanyun.pic.pipe.domain.PicPipeTrackPointGExample;
import com.deyuanyun.pic.pipe.domain.custom.CustomPicPipeIdentification;
import com.deyuanyun.pic.pipe.domain.exceldomain.GroundMarkVO;
import com.deyuanyun.pic.pipe.domain.exceldomain.TrackPointExcelImportVO;
import com.deyuanyun.pic.pipe.mapper.CustomPicPipeIdentificationMapper;
import com.deyuanyun.pic.pipe.mapper.PicPipeIdentificationGMapper;
import com.deyuanyun.pic.pipe.mapper.PicPipeIdentificationMapper;
import com.deyuanyun.pic.pipe.service.PipeIdentificationService;
import com.deyuanyun.pic.support.biz.CodeUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xu on 2016/1/21.
 */
@Service
public class PipeIdentificationServiceImpl implements PipeIdentificationService {
    static Logger logger=Logger.getLogger(PipeIdentificationServiceImpl.class);
    @Autowired
    private PicPipeIdentificationMapper identificationMapper;
    @Autowired
    private CustomPicPipeIdentificationMapper customIdentificationMapper;
    @Autowired
    private PicPipeIdentificationGMapper picPipeIdentificationGMapper;

    @Override
    public void insertIdentification(List<GroundMarkVO> groundMarkVOList) throws Exception{
        List<PicPipeIdentification> identifications= ConvertUtil.transform(groundMarkVOList,PicPipeIdentification.class);
       Date crDaye=new Date();
        for (PicPipeIdentification iden:identifications) {
            String id= iden.getId();
            if (StringUtils.isBlank(id)){
                iden.setId(CommonUtil.getUUID());
            }
            iden.setIsNewest((short)0);
            iden.setDeleted((short)0);
            iden.setCreateDate(crDaye);
            UserSessionVO userSessionVO= SessionCacheSupport2.getSessionVO();
            iden.setCreateUser(userSessionVO.getUsername());

            Map map=new HashMap();
            map.put("priUuid",iden.getId());
            List<GroundMarkVO> importVOs= MyCollectionUtil.filterListByMap(groundMarkVOList,map);
            Asserts.assertTrue(ObjectUtil.isNotNullEmpty(importVOs),"导入出错了，没有找到对应的坐标记录");
            GroundMarkVO importVO=importVOs.get(0);


            PicPipeIdentificationG g=new PicPipeIdentificationG();
            g.setDeleted((short)0);
            g.setIsNewest((short)0);
            g.setX(new BigDecimal(importVO.getLaocationX()));
            g.setY(new BigDecimal(importVO.getLaocationY()));
            g.setZ(new BigDecimal(importVO.getLaocationZ()));
            g.setRecorder(iden.getRecorder());
            g.setRecordtime(iden.getRecordtime());
            g.setPipeIdentificationId(iden.getId());
            g.setId(CommonUtil.getUUID());
            g.setConvertType("BD");

            g.setDataStated("20");
            picPipeIdentificationGMapper.insert(g);
            iden.setDataStated("20");
            identificationMapper.insert(iden);
        }

    }

    @Override
    public List<CustomPicPipeIdentification> queryIdentificationList(Map map)throws Exception{
        List<CustomPicPipeIdentification> identificationList= customIdentificationMapper.queryIdentificationList(map);
        CodeUtil.convertId2CodeName(identificationList,new String[]{"signType",
        "signStatus",
        "repairSuggestion",
        "repairstate"});
        return identificationList;
    }

    @Override
    public void delIdentificationRecord(String id){
        int i= identificationMapper.deleteByPrimaryKey(id);
        Asserts.assertTrue(i>0,"该记录已经不存在");

        PicPipeIdentificationGExample gExample=new PicPipeIdentificationGExample();
        gExample.createCriteria().andPipeIdentificationIdEqualTo(id);
        picPipeIdentificationGMapper.deleteByExample(gExample);

    }

}
